<template>
    <div class="views-changdiyuyuetongji">
        <div>
            <e-container>
                <el-form action="javascript:;" @submit="searchSubmit" inline>
                    <el-form-item label="场地名称">
                        <el-input v-model="search.changdimingcheng"></el-input>
                    </el-form-item>
                    <el-form-item label="类型">
                        <el-select v-model="search.leixing"
                            ><el-option label="请选择" value=""></el-option
                            ><e-select-option type="option" module="changdileixing" value="id" label="leixingmingcheng"></e-select-option
                        ></el-select>
                    </el-form-item>
                    <el-form-item label="预约时间">
                        <div style="display: flex">
                            <el-date-picker v-model="search.yuyueshijian_start" style="width: 120px" type="date" value-format="YYYY-MM-DD" placeholder="选择开始日期" />
                            -
                            <el-date-picker v-model="search.yuyueshijian_end" style="width: 120px" type="date" value-format="YYYY-MM-DD" placeholder="选择结束日期" />
                        </div>
                    </el-form-item>
                    <el-form-item label="预约状态">
                        <el-select v-model="search.yuyuezhuangtai"
                            ><el-option label="请选择" value=""></el-option><el-option label="待支付" value="待支付"></el-option>
                            <el-option label="已预约" value="已预约"></el-option>
                            <el-option label="已取消" value="已取消"></el-option>
                        </el-select> </el-form-item
                ></el-form>
            </e-container>
        </div>
        <div>
            <e-container>
                <div class="title-modelbox-widget1">
                    <h3 class="section-title">场地预约统计</h3>
                    <div class="sidebar-widget-body">
                        <div style="padding: 10px; background: #ffffff">
                            <el-table :data="changdiyuyueList">
                                <el-table-column prop="changdimingcheng" label="场地名称" width="180" />
                                <el-table-column prop="sum_changdijiage" label="场地价格" sortable />
                                <el-table-column prop="count" label="场地预约数量" sortable />
                            </el-table>
                        </div>

                        <v3-echarts :options="barOptions" width="100%" height="300px"></v3-echarts>

                        <v3-echarts :options="barOptions1" width="100%" height="300px"></v3-echarts>
                    </div>
                    <!-- /.sidebar-widget-body -->
                </div>
            </e-container>
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";

    import { ref, reactive, watch, computed, unref } from "vue";
    import { useRoute } from "vue-router";
    import { session } from "@/utils/utils";
    import { extend } from "@/utils/extend";

    const route = useRoute();

    const search = reactive({ changdimingcheng: "", leixing: "", yuyueshijian_start: "", yuyueshijian_end: "", yuyuezhuangtai: "" });
    extend(search, route.query);
    const serachSubmit = () => {
        loadDatabaseList();
    };
    var timer;
    watch(
        () => search,
        () => {
            clearTimeout(timer);
            timer = setTimeout(() => {
                serachSubmit();
            }, 200);
        },
        { deep: true }
    );
    const changdiyuyueList = ref([]);
    const loadDatabaseList = async () => {
        var where = " 1=1 ";
        if (search.changdimingcheng) {
            where += ` AND cdy.changdimingcheng LIKE '%${search.changdimingcheng}%' `;
        }
        if (search.leixing) {
            where += ` AND cdy.leixing='${search.leixing}' `;
        }
        if (search.yuyueshijian_start) {
            where += ` AND cdy.yuyueshijian>='${search.yuyueshijian_start}' `;
        }
        if (search.yuyueshijian_end) {
            where += ` AND cdy.yuyueshijian<='${search.yuyueshijian_end}' `;
        }
        if (search.yuyuezhuangtai) {
            where += ` AND cdy.yuyuezhuangtai='${search.yuyuezhuangtai}' `;
        }
        changdiyuyueList.value = await DB.name("changdiyuyue")
            .alias("cdy")
            .field("cdy.changdimingcheng")
            .group("cdy.changdimingcheng")
            .field("sum(cdy.changdijiage) sum_changdijiage")
            .field("count(*) count")
            .where(where)
            .select();
    };

    loadDatabaseList();
    const barOptions = computed(() => {
        var list = unref(changdiyuyueList);
        return {
            title: {
                text: "",
            },
            tooltip: {
                trigger: "axis",
                axisPointer: {
                    type: "shadow",
                },
            },
            grid: {
                left: "3%",
                right: "4%",
                bottom: "3%",
                containLabel: true,
            },
            xAxis: [
                {
                    type: "category",
                    data: list.map((s) => s.changdimingcheng),
                    axisTick: {
                        alignWithLabel: true,
                    },
                },
            ],
            yAxis: [
                {
                    type: "value",
                },
            ],
            series: [
                {
                    name: "场地价格",
                    type: "bar",
                    barWidth: "20px",
                    data: list.map((s) => parseFloat(s.sum_changdijiage)),
                },
                {
                    name: "场地预约数量",
                    type: "bar",
                    barWidth: "20px",
                    data: list.map((s) => parseFloat(s.count)),
                },
            ],
        };
    });
    const barOptions1 = computed(() => {
        var list = unref(changdiyuyueList);
        return {
            title: {
                text: "",
                left: "center",
            },
            tooltip: {
                trigger: "item",
                formatter: "{b}：{c},{d}%",
            },
            legend: {
                orient: "vertical",
                left: "left",
            },
            series: [
                {
                    name: "场地价格",
                    type: "pie",
                    radius: "50%",
                    data: list.map((s) => {
                        return {
                            value: parseFloat(s.sum_changdijiage),
                            name: s.changdimingcheng,
                        };
                    }),
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: "rgba(0, 0, 0, 0.5)",
                        },
                    },
                },
            ],
        };
    });
</script>

<style scoped lang="scss">
    .views-changdiyuyuetongji {
    }
</style>
